import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import Components from "unplugin-vue-components/vite";
import { NaiveUiResolver } from "unplugin-vue-components/resolvers";
import { viteMockServe } from "vite-plugin-mock";
import setupConfig from "./config";
import cssInjectedByJsPlugin from "vite-plugin-css-injected-by-js";
import viteCompression from "vite-plugin-compression";

export default defineConfig(({ mode }) => {
  const { build } = setupConfig({ mode });
  const plugins = [];
  if (mode === "lib") {
    plugins.push(cssInjectedByJsPlugin());
  } else {
    plugins.push(
      viteCompression({
        threshold: 10240,
      })
    );
  }

  const env = loadEnv(mode, process.cwd());
  return {
    root: path.resolve(__dirname, "src/app"), // 修改root参数为多页面的根目录
    base: "./",
    plugins: [
      vue(),
      Components({
        resolvers: [NaiveUiResolver()],
      }),
      viteMockServe({
        mockPath: "./mock",
        localEnabled: false, // 是否启用
        prodEnabled: false, // 生产环境打包开关
      }),
      ...plugins,
    ],
    publicDir: "public",
    define: {
      // 【从 2.0.0-beta.70 开始，string 值会以原始表达式形式使用，所以如果定义了一个字符串常量，它需要被显式地打引号。（例如使用 JSON.stringify）】
      VITE_APP_BASE_API: JSON.stringify(env.VITE_APP_BASE_API),
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
        __ROOT__: path.resolve(__dirname, ""),
      },
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"], // 自动匹配文件后缀名
    },
    build,
    server: {
      host: "0.0.0.0",
      port: 8280,
      https: false,
      proxy: {
        "^/api": {
          target: "https://vite.itnavs.com/",
          changeOrigin: true,
        },
        "^/bic": {
          target: "https://api.vipbic.com/",
          changeOrigin: true,
          rewrite: (path: any) => path.replace(/^\/bic/, ""),
        },
      },
    },
  }
})

